package com.hhhn.wk.base;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.hhhn.wk.utils.ToastUtils;
import com.umeng.analytics.MobclickAgent;

/**
 * * * * * * * * * * * *
 * @author：WenKe ｡◕‿◕｡ *
 * Data：2017     ｡◕‿◕｡ *
 * QQ：370820580  ｡◕‿◕｡ *
 * Function：     ｡◕‿◕｡ *
 *BaseFragment
 * * * * * * * * * * * */
public abstract class BaseFragment extends Fragment
{
    //贴附的activity
    protected BaseActivity mActivity;
    //根view
    protected View mRootView;
    //是否对用户可见
    protected boolean mIsVisible;
    //是否加载完成
    //当执行完oncreatview,View的初始化方法后方法后即为true
    protected boolean mIsPrepare;
    @Override
    public void onAttach(Context context)
    {
        super.onAttach(context);
        mActivity = (BaseActivity)getActivity();
    }
    @Override
    @Nullable
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState)
    {
        mRootView = inflater.inflate(setLayoutResouceId(), container, false);
        initView(mRootView);
        initData();
        mIsPrepare = true;
        onLazyLoad();
        initClick();
        return mRootView;
    }




    //初始化View
    protected abstract void initView(View mRootView);
    //初始化数据
    protected abstract void initData();
    //设置监听事件
    protected abstract void initClick();
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser)
    {
        super.setUserVisibleHint(isVisibleToUser);
        this.mIsVisible = isVisibleToUser;
        if (isVisibleToUser)
        {
            onVisibleToUser();
        }
    }
    //用户可见时执行的操作
    protected void onVisibleToUser()
    {
        if (mIsPrepare && mIsVisible)
        {
            onLazyLoad();
        }
    }
    //懒加载，仅当用户可见切view初始化结束后才会执行
    protected void onLazyLoad()
    {

    }
    @SuppressWarnings("unchecked")
    protected <T extends View> T findViewById(int id)
    {
        if (mRootView == null)
        {
            return null;
        }
        return (T) mRootView.findViewById(id);
    }
    //设置根布局资源id
    protected abstract int setLayoutResouceId();

    //封装Tost
    public void toastWk(String text) {
        ToastUtils.showShortToast(mActivity,text);
    }


//    @Override
//    public void onResume() {
//        super.onResume();
//        MobclickAgent.onResume(mActivity);
//    }
//
//    @Override
//    public void onPause() {
//        super.onPause();
//        MobclickAgent.onPause(mActivity);
//    }
}